var request;
var userActionData;
var collectorUrl = "servlet/ActionCollector";


/** Request对象封装函数
参数：
reqType: HTTP请求类型，例如 GET 或 POST.
url    : 服务器端程序的URL
asynch : 是否异步发送请求 
*/
function httpRequest(reqType, url, asynch) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveObject("Msxml2.XMLHTTP");
		if (!request) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (request) {
		initReq(reqType, url, asynch);
	} else {
		alert("Your browser does not permit ajax");
	}
}

/*初始化已经创建的request对象*/
function initReq(reqType, url, bool) {
	request.onreadystatechange = null ;  // = dealResponse;  回调函数   
	request.open(reqType, url, bool);
	request.setRequestHeader("Content-Type", 
		"application/x-www-form-urlencoded;charset=UTF-8");
	request.send(userActionData);
}


/*将用户点击结果页面的信息发送到服务器端收集
 * anchor : 点击节点
 * */
function onClickData(anchor) {

	var pageTitle = anchor.innerHTML;   // 结果页面标题
	var pageUrl = anchor.href;    // 结果页面链接
	var queryWord = $("input[name=query]").val();
	
	var pNode = anchor.parentNode;
	//alert(pNode.nodeName);	
	var p1Node = pNode.parentNode;
	//alert(p1Node.nodeName);
	var p2Node = p1Node.parentNode;
	//alert(p2Node.nodeName);
	
	var p3Node = p2Node.nextSibling;
	//alert(summaryFontNode.nodeName);
	var p4Node = p3Node.firstChild;
	var summaryFontNode = p4Node.firstChild;
	var summary = summaryFontNode.innerHTML;
	summary = summary.replace(/<\/?[^>]*>/g,'');
	//alert(summary);
	
	userActionData = "&pageTitle="+pageTitle+"&pageUrl="+pageUrl+"&queryWord="+queryWord+"&summary="+summary;

	httpRequest("POST", collectorUrl, true);
	//alert("success");
}


